package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class CheckItemController {
    @Autowired
    CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/checkitem/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            log.info("新增检查项名为：{}", checkItem.getName());
            checkItemService.addCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/checkitem/delete")
    public Result deleteCheckItem(String id) {
        try {
            log.info("根据id删除检查项：{}", id);
            checkItemService.deleteCheckItem(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @GetMapping("/checkitem/findById")
    public Result findByIdCheckItem(String id) {
        CheckItem checkItem = checkItemService.findByIdCheckItem(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 更新检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/checkitem/edit")
    public Result updateCheckItem(@RequestBody CheckItem checkItem) {
        try {
            log.info("需要修改的检查项id为：{}", checkItem.getId());
            checkItemService.updateCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @PostMapping("/checkitem/findPage")
    public PageResult selectPageCheckItem(@RequestBody QueryPageBean queryPageBean) {
        PageResult checkItems = checkItemService.selectPageCheckItem(queryPageBean);

        return checkItems;
    }

}
